package com.fingard.avatar.website.controller;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.URLUtil;
import com.fingard.avatar.website.exception.BizException;
import com.fingard.avatar.website.model.bean.ResultContext;
import com.fingard.avatar.website.model.dto.IdDto;
import com.fingard.avatar.website.model.po.SysUploadPo;
import com.fingard.avatar.website.service.SysUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author 徐一涛
 * @date 2022-04-27
 */
@Slf4j
@RestController
@RequestMapping("api/file")
@Api(tags = "文件管理")
public class SysUploadController {

    @Resource
    protected SysUploadService sysUploadService;


    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public ResultContext<SysUploadPo> upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String newName = String.format("%s.%s", IdUtil.fastUUID(), FileUtil.getSuffix(fileName));
        String url =" clientService.upload(newName, file.getInputStream())";
        return ResultContext.success(sysUploadService.add(fileName, newName, url));
    }

}
